'''
@author : Kevincat
function:
Data mapping from standard RSSI to distance 
Rendering of standard RSSI

'''
from kc.wp.models import *

# mapping from standard RSSI to distance(unit: m) 
RSSI_DISTANCE = {
# rssi:distance
	0: 1,
	1: 10,
	2: 30,
	3: 40,
	4: 50,
	100: 10,
}

# let's define a model to store test result

ssid_mac_1 = {'kc':'1c:af:f7:a7:89:10',
			'507':'00:1d:0f:66:e2:12',
			'M3-Tplink':'00:0a:eb:e1:16:96',
			'TP-LINK_6086F4':'74:ea:3a:60:86:f4'}
# we should keep an eye on the zero rssi!
'''by Dell Wireless Card'''
raw_data_1 = """
			200,3800|0,-42,-80;
			200,2800|0,-45,-78;
			1400,3800|-82,36,77;
			1400,2800|-80,38,72;
			2600,3800|-79,55,74;
			2600,2800|-83,52,76;
			3800,3800|-76,53,61;
			3800,2800|-77,53,69;
			5000,3800|-79,49,70;
			5000,2800|-74,52,62;
			6200,3800|-75,61,56;
			6200,2800|-72,62,60;
			7400,3800|-70,58,56;
			7400,2800|-75,60,56;
			8600,3800|-68,73,61;
			8600,2800|-60,66,79;
			9800,3800|-64,60,51;
			9800,2800|58,69,50;
			11000,3800|62,56,53;
			11000,2800|55,68,52;
			12200,3800|62,69,57;
			12200,2800|50,66,53;
			13400,3800|35,69,63;
			13400,2800|50,67,62;
			"""
'''by TP Link TLWB321G V1'''
raw_data_1_1 = """ 
			13200,3800|50,66,64,80;
			13200,2800|52,76,60,82;
			11900,3800|62,72,68,76;
			11900,2800|58,68,62,78;
			10600,3800|76,64,52,78;
			10600,2800|73,72,68,76;
			9300,3800|72,60,52,74;
			9300,2800|80,66,68,70;
			8000,3800|72,62,64,72;
			8000,2800|76,68,64,78;
			6700,3800|76,60,62,72;
			6700,2800|80,60,66,70;
			5400,3800|82,62,66,60;
			5400,2800|84,58,68,62;
			4100,3800|0,50,76,50;
			4100,2800|86,56,72,55;
			2800,3800|84,54,68,58;
			2800,2800|86,46,76,60;
			1500,3800|86,44,76,60;
			1500,2800|82,46,72,68;
			200,3800|0,52,0,68;
			200,2800|86,48,76,70;
			##511|;
			12650,4800|48,82,56,84;
			12650,7230|40,80,50,66;
			11920,7230|46,80,46,68;
			11920,4800|50,72,64,86;
			##510|;
			10020,4800|64,76,48,70;
			10020,7230|58,76,50,58;
			9090,7230|64,84,32,58;
			9090,4800|68,68,50,72;
			##509|;
			7290,4800|0,0,0,0;
			7290,7200|0,0,0,0;
			6360,7200|0,0,0,0;
			6360,4800|0,0,0,0;
			##508|;
			4560,4800|80,54,66,52;
			4560,7200|76,60,54,42;
			3630,7200|80,56,68,40;
			3630,4800|84,52,68,48;
			##507|;
			1830,4800|0,0,0,0;
			1830,7200|0,0,0,0;
			1000,7200|0,0,0,0;
			1000,4800|0,0,0,0
"""

ap_location = """
			11620,7200 : kc
			8520,7100 : M3-TPLINK
			3230,7400 : TPLINK-6*
			800,4600 : 507
"""

def add_data():
	r = raw_data_1_1.split(';')
	for _r in r:
		l = _r.split('|')
		loc = l[0].split(',')
		rssi = l[1].split(',')
		try:
			print loc
			s = SamplePointLoc(field_id = 0, x = int(loc[0]), y = int(loc[1]))
			s.save()
			i = 0
			for _rssi in rssi:
				if i == 0:
					_mac = ssid_mac_1['kc']
				elif i == 1:
					_mac = ssid_mac_1['507']
				elif i == 2:
					_mac = ssid_mac_1['M3-Tplink']
				elif i == 3:
					_mac = ssid_mac_1['TP-LINK_6086F4']
				
				_rssi = int(_rssi)
				
				if _rssi > 0:
					_rssi = -_rssi
				elif _rssi == 0:
					_rssi = -86
				ss = SamplePointRssi(sample_point = s, rssi = _rssi, mac = _mac)
				i = i+1
				ss.save()
		except:
			print "skip this row!"

ssid_mac_2 = {'N1':'00:24:b2:27:ad:e6',
			'N2':'00:24:b2:7e:df:fc',
			'N3':'00:22:3f:33:06:a8',
			'N4':'00:22:3f:12:52:5e'}

# N1,N2,N3,N4
raw_data_2 = """
			39224,23143|32,82,81,65;
			39224,23858|34,82,81,65;
			39224,24454|51,85,78,68;
			39227,23450|36,74,73,67;
			39882,29349|68,85,70,71;
			40020,29701|61,76,76,74;
			38997,29701|68,86,75,66;
			37973,29701|66,79,75,77;
			36764,32052|75,79,77,78;
			35722,32052|66,86,75,78;
			34699,32052|65,78,81,71;
			37666,33688|63,84,74,74;
			36643,33688|66,83,80,81;
			35825,33688|68,85,80,77;
			34597,33688|68,77,82,76;
			34699,35426|72,73,80,76;
			35722,35426|71,83,78,76;
			36643,35426|74,85,75,73;
			37769,35426|66,86,74,72;
			34699,37062|70,83,75,69;
			35620,37062|67,82,79,80;
			36746,37062|76,80,75,70;
			37769,37062|64,82,84,70
"""

def add_data_2():
	r = raw_data_2.split(';')
	for _r in r:
		l = _r.split('|')
		loc = l[0].split(',')
		rssi = l[1].split(',')
		s = SamplePointLoc(field_id = 2, x = int(loc[0]), y = int(loc[1]))
		s.save()
		i = 0
		for _rssi in rssi:
			if i == 0:
				_mac = ssid_mac_2['N1']
			elif i == 1:
				_mac = ssid_mac_2['N2']
			elif i == 2:
				_mac = ssid_mac_2['N3']
			elif i == 3:
				_mac = ssid_mac_2['N4']
			
			_rssi = int(_rssi)
			
			if _rssi > 0:
				_rssi = -_rssi
			elif _rssi == 0:
				_rssi = -90
			ss = SamplePointRssi(sample_point = s, rssi = _rssi, mac = _mac)
			i = i+1
			ss.save()

field_data_0 = """
	1|0,0;5190,0;10920,0;11811,189;12475,644;12919,1283;13120,2200;13120,2700;13120,3900;13650,3900;13650,8230;0,8230||
	1|-7354,1936;-3303,9099;-15185,15819;-17316,12050;-16855,11789;-17692,10309;-17964,9462;-17897,8690;-17446,7874;-16860,7311;-7354,1936||
	2|200,4000;2630,4000;2630,8030;200,8030||
	2|2830,4000;5360,4000;5360,8030;2830,8030||
	2|5560,4000;8090,4000;8090,8030;5560,8030||
	2|8290,4000;10820,4000;10820,8030;8290,8030||
	2|11020,4000;13450,4000;13450,8030;11020,8030||
	2|-15109,15546;-17086,12050;-14978,10842;-12994,14350||
	2|-14804,10733;-12601,9498;-10618,13006;-12820,14252||
	2|-12427,9400;-10225,8154;-8241,11662;-10443,12908||
	2|-8067,11564;-10051,8056;-7849,6810;-5865,10318||
	2|-7675,6704;-5560,5515;-3576,9024;-5691,10220||
	3|200,2800;12920,2800;12920,3800;200,3800||
	3|-17222,10733;-6150,4471;-5730,5330;-16730,11603||
	4|200,200;5090,200;5090,2600;200,2600||
	4|5290,200;10820,200;10820,2600;5290,2600||
	4|0,5420;-1222,5420;-2423,5734;-3584,6065;-4686,6653;-6025,4286;-4608,3601;-3122,3079;-1571,2801;0,2700||
	4|-15493,9525;-16674,7436;-11861,4714;-10679,6803||
	4|-10505,6704;-6249,4286;-7355,2208;-11687,4623
"""

field_data_1 = """
1|1312.3730,62005.2065;13780.0681,62005.2065;13780.0681,60793.0790;28269.5519,60793.0790;28269.5519,65372.2270;41141.6048,65372.2270;41141.6048,61197.1215;43028.6073,61197.1215;43208.6073,56819.9947;43972.1084,56819.9947;43972.8911,11364.6780;34691.0959,11364.6780;34691.0959,12750.5317;31917.2262,12750.5317;31917.2262,3902.3886;22208.6819,3902.3886;22208.6819,12750.5317;2392.8053,12750.5317;2392.8053,21234.9868;1402.7158,21234.9868;1312.3730,62005.2065||
2|1938.8315,61240.6820;13340.7631,61240,6820;13340.7631,56579.8916;42215.7838,56579.8916;42215.7838,21486.5987;41256.9701,21486.5987;41256.9701,19267.1747;35037.7350,19267.1747;35037.7350,13469.6723;2534.8266,13469.6723;2534.8266,56286.3082;1938.8315,56286.3082;1938.8315,61240.6820||
3|8562.7836,46312.1415;18753.5263,46312.1415;18753.5263,45855.2354;8562.7836,45855.2354;8562.7836,46312.1415||
3|8562.7836,44810.8789;18753.5263,44810.8789;18753.5263,44288.7007;8562.7836,44288.7007;8562.7836,44810.8789||
3|8562.7836,43113.7997;18753.5263,43113.7997;18753.5263,42656.8938;8562.7836,42656.8938;8562.7836,43113.7997||
3|8562.7836,41481.9926;18753.5263,41481.9926;18753.5263,41025.0866;8562.7836,41025.0866;8562.7836,41481.9926||
3|8562.7836,39850.1856;18753.5263,39850.1856;18753.5263,39458.5519;8562.7836,39458.5519;8562.7836,39850.1856||
3|8562.7836,38218.3787;18753.5263,38218.3787;18753.5263,37696.2003;8562.7836,37696.2003;8562.7836,38218.3787||
3|8562.7836,36521.2993;18753.5263,36521.2993;18753.5263,36064.3934;8562.7836,36064.3934;8562.7836,36521.2993||
3|8562.7836,34954.7646;18753.5263,34954.7646;18753.5263,34432.5862;8562.7836,34432.5862;8562.7836,34954.7646||
3|8562.7836,33322.9575;18753.5263,33322.9575;18753.5263,32800.7793;8562.7836,32800.7793;8562.7836,33322.9575||
3|8562.7836,31625.8782;18753.5263,31625.8782;18753.5263,31168.9722;8562.7836,31168.9722;8562.7836,31625.8782||
3|24501.6451,54536.4489;37762.2552,54536.4489;37762.2552,54014.2705;24501.6451,54014.2705;24501.6451,54536.4489||
3|24501.6451,53035.1864;37762.2552,53035.1864;37762.2552,52513.0081;24501.6451,52513.0081;24501.6451,53035.1864||
3|24501.6451,51533.9240;37762.2552,51533.9240;37762.2552,51011.7456;24501.6451,51011.7456;24501.6451,51533.9240||
3|24501.6451,50032.6615;37762.2552,50032.6615;37762.2552,49510.4832;24501.6451,49510.4832;24501.6451,50032.6615||
3|24501.6451,48531.3991;37762.2552,48531.3991;37762.2552,48009.2207;24501.6451,48009.2207;24501.6451,48531.3991||
3|33973.5094,47030.1366;37762.2552,47030.1366;37762.2552,46507.9583;33973.5094,46507.9583;33973.5094,47030.1366||
3|33973.5094,45528.8740;37762.2552,45528.8740;37762.2552,45006.6958;33973.5094,45006.6958;33973.5094,45528.8740||
3|33973.5094,44027.6115;37762.2552,44027.6115;37762.2552,43505.4334;33973.5094,43505.4334;33973.5094,44027.6115||
3|33973.5094,42526.3491;37762.2552,42526.3491;37762.2552,42004.1709;33973.5094,42004.1709;33973.5094,42526.3491||
3|33973.5094,41025.0866;37762.2552,41025.0866;37762.2552,40568.1807;33973.5094,40568.1807;33973.5094,41025.0866||
3|34688.4284,38215.0519;37823.9422,38215.0519;37823.9422,37562.3290;34688.4284,37562.3290;34688.4284,38215.0519||
3|34688.4284,36583.2447;37823.9422,36583.2447;37823.9422,35930.5221;34688.4284,35930.5221;34688.4284,36583.2447||
3|34688.4284,34951.4378;37823.9422,34951.4378;37823.9422,34298.7149;34688.4284,34298.7149;34688.4284,34951.4378||
3|34688.4284,33254.3584;37823.9422,33254.3584;37823.9422,32601.6356;34688.4284,32601.6356;34688.4284,33254.3584||
3|34688.4284,31622.5515;37823.9422,31622.5515;37823.9422,31969.8286;34688.4284,31969.8286;34688.4284,31622.5515||
3|5096.2515,29916.6739;12673.7429,29916.6739;12673.7429,29453.7680;5096.2515,29453.7680;5096.2515,29916.6739||
3|5096.2515,28409.4114;12673.7429,28409.4114;12673.7429,27952.5055;5096.2515,27952.5055;5096.2515,28409.4114||
3|5096.2515,26973.4212;12673.7429,26973.4212;12673.7429,26451.2431;5096.2515,26451.2431;5096.2515,26973.4212||
3|5096.2515,25472.1588;12673.7429,25472.1588;12673.7429,24949.9806;5096.2515,24949.9806;5096.2515,25472.1588||
3|5096.2515,23970.8963;12673.7429,23970.8963;12673.7429,23448.7182;5096.2515,23448.7182;5096.2515,23970.8963||
4|3003.1659,52251.3452;7053.2046,52251.3452;7053.2046,51272.2609;3003.1659,51272.2609;3003.1659,52251.3452||
4|3003.1659,49053.0033;7053.2046,49053.0033;7053.2046,48073.9191;3003.1659,48073.9191;3003.1659,49053.0033||
4|3003.1659,44092.3099;7053.2046,44092.3099;7053.2046,43113.2258;3003.1659,43113.2258;3003.1659,44092.3099||
4|3003.1659,40776.5925;7053.2046,40776.5925;7053.2046,39945.7035;3003.1659,39945.7035;3003.1659,40776.5925||
4|3003.1659,35866.7638;7053.2046,35866.7638;7053.2046,34884.8340;3003.1659,34884.8340;3003.1659,35866.7638||
4|3003.1659,32694.3084;7053.2046,32694.3084;7053.2046,31787.8839;3003.1659,31787.8839;3003.1659,32694.3084||
4|39417.8550,52251.3452;41761.2781,52251.3452;41761.2781,51272.2609;39417.8550,51272.2609;39417.8550,52251.3452||
4|39417.8550,49053.0033;41761.2781,49053.0033;41761.2781,48073.9191;39417.8550,48073.9191;39417.8550,49053.0033||
4|39417.8550,44092.3099;41761.2781,44092.3099;41761.2781,43113.2258;39417.8550,43113.2258;39417.8550,44092.3099||
4|39417.8550,40776.5925;41761.2781,40776.5925;41761.2781,39945.7035;39417.8550,39945.7035;39417.8550,40776.5925||
4|39417.8550,35866.7638;41761.2781,35866.7638;41761.2781,34884.8340;39417.8550,34884.8340;39417.8550,35866.7638||
4|39417.8550,32694.3084;41761.2781,32694.3084;41761.2781,31787.8839;39417.8550,31787.8839;39417.8550,32694.3084||
4|8735.6584,52251.3452;13568.8996,52251.3452;13568.8996,51272.2609;8735.6584,51272.2609;8735.6584,52251.3452||
4|8735.6584,49053.0033;13568.8996,49053.0033;13568.8996,48073.9191;8735.6584,48073.9191;8735.6584,49053.0033||
4|16112.7106,52251.3452;20861.1578,52251.3452;20861.1578,51272.2609;16112.7106,51272.2609;16112.7106,52251.3452||
4|16112.7106,49053.0033;20861.1578,49053.0033;20861.1578,48073.9191;16112.7106,48073.9191;16112.7106,49053.0033||
4|16112.7106,55447.0146;20861.1578,55447.0146;20861.1578,54345.5559;16112.7106,54345.5559;16112.7106,55447.0146||
4|20776.3641,45025.5196;22133.0633,45025.5196;22133.0633,44008.7885;20776.3641,44008.7885;20776.3641,45025.5196||
4|20776.3641,40450.2292;22133.0633,40450.2292;22133.0633,39433.4981;20776.3641,39433.4981;20776.3641,40450.2292||
4|20776.3641,35620.7560;22133.0633,35620.7560;22133.0633,34688.7523;20776.3641,34688.7523;20776.3641,35620.7560||
4|20776.3641,32485.8348;22133.0633,32485.8348;22133.0633,31469.1035;20776.3641,31469.1035;20776.3641,32485.8348||
4|31545.1640,45025.5196;32901.8634,45025.5196;32901.8634,44008.7885;31545.1640,44008.7885;31545.1640,45025.5196||
4|31545.1640,40450.2292;32901.8634,40450.2292;32901.8634,39433.4981;31545.1640,39433.4981;31545.1640,40450.2292||
4|31545.1640,35620.7560;32901.8634,35620.7560;32901.8634,34688.7523;31545.1640,34688.7523;31545.1640,35620.7560||
4|31545.1640,32485.8348;32901.8634,32485.8348;32901.8634,31469.1035;31545.1640,31469.1035;31545.1640,32485.8348||
4|32990.4660,27711.9036;35364.6896,27711.9036;35364.6896,26695.1723;32990.4660,26695.1723;32990.4660,27711.9036||
4|36297.4203,27711.9036;38671.6442,27711.9036;38671.6442,26695.1723;36297.4203,26695.1723;36297.4203,27711.9036||
4|39689.1686,27711.9036;42063.3921,27711.9036;42063.3921,26695.1723;39689.1686,26695.1723;39689.1686,27711.9036||
4|32990.4660,24407.5272;35364.6896,24407.5272;35364.6896,23560.2511;32990.4660,23560.2511;32990.4660,24407.5272||
4|36297.4203,24407.5272;38671.6442,24407.5272;38671.6442,23560.2511;36297.4203,23560.2511;36297.4203,24407.5272||
4|39689.1686,24407.5272;42063.3921,24407.5272;42063.3921,23560.2511;39689.1686,23560.2511;39689.1686,24407.5272||
4|23663.1590,27711.9036;30870.6232,27711.9036;30870.6232,26695.1723;23663.1590,26695.1723;23663.1590,27711.9036||
4|15268.5827,28135.5415;20101.8236,28135.5415;20101.8236,27118.8104;15268.5827,27118.8104;15268.5827,28135.5415||
4|15268.5827,24915.8928;20101.8236,24915.8928;20101.8236,23899.1615;15268.5827,23899.1615;15268.5827,24915.8928
"""

def add_map_data():
	r = field_data_1.split('||')
	for _r in r:
		__r = _r.split('|')
		color = __r[0]
		points = __r[1]
		bp = BuildingPoint(field_id = 2, color = color, points = points)
		bp.save()

# dump commands
		
def dump_umr():
	q = UserMacRssi.objects.all()
	for _q in q:
		_q.delete()
		
def dump_ul(field_id):
	q = UserLocation.objects.filter(field_id = field_id)
	for _q in q:
		#if _q.count < 1100 and _q.count > 10:
		if _q.count > 0:
			_q.delete()
			
def dump_rssi(field_id):
	sp = SamplePointLoc.objects.filter(field_id = field_id)
	for _sp in sp:
		sr = SamplePointRssi.objects.filter(sample_point = _sp)
		for _sr in sr:
			_sr.delete()
		_sp.delete()
	print "deleted all rssi data"